
#import "DataType.h"
#import "DataSL.h"

void gLoadData()
{
	NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
	if([userDefault boolForKey:@"alreadySaved"]) {
		// 已经有存盘数据了，读取之
		NSString* version = [userDefault stringForKey:@"Version"];
		// 为版本更新改变存储内容做准备
		if([version isEqualToString:@"1.0"]) {
			// 设定数据
			gSettingData.musicOn = [userDefault boolForKey:@"MusicOn"];
			gSettingData.soundOn = [userDefault boolForKey:@"SoundOn"];
			gSettingData.spellInRight = [userDefault boolForKey:@"SpellInRight"];
			gSettingData.operationType = [userDefault integerForKey:@"OperationType"];
			gSettingData.startPosX = gSettingData.spellInRight ? 0 : 50;
		
			// 全局数据
			gGlobalData.gold = [userDefault integerForKey:@"Gold"];
			gGlobalData.highScoreTime = [userDefault integerForKey:@"HighScoreTime"];
			gGlobalData.highScoreSurvival = [userDefault integerForKey:@"HighScoreSurvival"];
			for(int i=0; i<INVADERS_COUNT; i++)
				gGlobalData.invadersCount[i] = [userDefault integerForKey:[NSString stringWithFormat:@"InvaderCount%02d", i]];
			gGlobalData.totalInvaders = [userDefault integerForKey:@"TotalInvaders"];
			gGlobalData.perfectFinish = [userDefault integerForKey:@"PerfectFinish"];
			gGlobalData.highLastInSurvival = [userDefault integerForKey:@"HighLastSurvival"];
			gGlobalData.sendToSocial = [userDefault boolForKey:@"SendToSocial"];
			gGlobalData.playTimes = [userDefault integerForKey:@"PlayTimes"];
			gGlobalData.totalBuyCount = [userDefault integerForKey:@"TotalBuyCount"];
			gGlobalData.reviewTimes = [userDefault integerForKey:@"ReviewTimes"];
			
			// 魔法装备情况
			for(int i = 0; i < SPELLSLOT_COUNT; i++) {
				gGlobalData.spells[i] = (SpellType)[userDefault integerForKey:[NSString stringWithFormat:@"SpellSlot%d", i]];
			}
		}
	} else {
		// 初始化数据
		// 设定数据
		gSettingData.musicOn = true;
		gSettingData.soundOn = true;
		gSettingData.spellInRight = true;
		gSettingData.operationType = 0;
		gSettingData.startPosX = gSettingData.spellInRight ? 0 : 50;
		
		// 全局数据
		gGlobalData.gold = 1000;
		gGlobalData.highScoreTime = 0;
		gGlobalData.highScoreSurvival = 0;
		for(int i=0; i<INVADERS_COUNT; i++)
			gGlobalData.invadersCount[i] = 0;
		gGlobalData.totalInvaders = 0;
		gGlobalData.perfectFinish = 0;
		gGlobalData.highLastInSurvival = 0;
		gGlobalData.sendToSocial = false;
		gGlobalData.playTimes = 0;
		gGlobalData.totalBuyCount = 0;
		gGlobalData.reviewTimes = 3;
		
		// 魔法装备情况
		for(int i = 0; i < SPELLSLOT_COUNT; i++) {
			gGlobalData.spells[i] = ST_None;
		}
		
		gSaveData();
	}	
}

void gSaveData()
{
	NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
	[userDefault setBool:true forKey:@"alreadySaved"];
	[userDefault setObject:@"1.0" forKey:@"Version"];

	// 设定数据
	[userDefault setBool:gSettingData.musicOn forKey:@"MusicOn"];
	[userDefault setBool:gSettingData.soundOn forKey:@"SoundOn"];
	[userDefault setBool:gSettingData.spellInRight forKey:@"SpellInRight"];
	[userDefault setInteger:gSettingData.operationType forKey:@"OperationType"];
			
	// 全局数据
	[userDefault setInteger:gGlobalData.gold forKey:@"Gold"];
	[userDefault setInteger:gGlobalData.highScoreTime forKey:@"HighScoreTime"];
	[userDefault setInteger:gGlobalData.highScoreSurvival forKey:@"HighScoreSurvival"];
	for(int i=0; i<INVADERS_COUNT; i++)
		[userDefault setInteger:gGlobalData.invadersCount[i] forKey:[NSString stringWithFormat:@"InvaderCount%02d", i]];
	[userDefault setInteger:gGlobalData.totalInvaders forKey:@"TotalInvaders"];
	[userDefault setInteger:gGlobalData.perfectFinish forKey:@"PerfectFinish"];
	[userDefault setInteger:gGlobalData.highLastInSurvival forKey:@"HighLastSurvival"];
	[userDefault setBool:gGlobalData.sendToSocial forKey:@"SendToSocial"];
	[userDefault setInteger:gGlobalData.playTimes forKey:@"PlayTimes"];
	[userDefault setInteger:gGlobalData.totalBuyCount forKey:@"TotalBuyCount"];
	[userDefault setInteger:gGlobalData.reviewTimes forKey:@"ReviewTimes"];
																		
	// 魔法装备情况
	for(int i = 0; i < SPELLSLOT_COUNT; i++) {
		[userDefault setInteger:gGlobalData.spells[i] forKey:[NSString stringWithFormat:@"SpellSlot%d", i]];
	}
	
	[userDefault synchronize];
}